//
//  TYZBasePickerView.m
//  BeautyIndustry
//
//  Created by tangbin on 2020/12/25.
//

#import "TYZBasePickerView.h"
#import "UIWindow+BasePickerView.h"

@interface TYZBasePickerView () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (nonatomic, strong) EDSCustomLeftRightBtnView *topView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSArray *pickerList;

@property (nonatomic, strong) NSArray *pickerTwoList;

@property (nonatomic, copy) NSString *value;

@property (nonatomic, copy) NSString *valueTwo;

/// 0 表示只有一组数据；1表示有两组数据
@property (nonatomic, assign) NSInteger type;

@end

@implementation TYZBasePickerView

- (instancetype)initWithFrame:(CGRect)frame type:(NSInteger)type
{
    self.type = type;
    return [self initWithFrame:frame];
}

- (void)initWithSubView
{
    self.backgroundColor = [UIColor whiteColor];
    [self initWithTopView];
    
    [self initWithPickerView];
}

- (void)setTitle:(NSString *)title
{
    _topView.titleLabel.text = title;
}

- (void)initWithTopView
{
    CGRect frame = CGRectMake(0, 0, kScreenWidth, kEDSCustomLeftRightBtnViewHeight);
    _topView = [[EDSCustomLeftRightBtnView alloc] initWithFrame:frame leftTitle:@"取消" leftColor:[UIColor whiteColor] rightTitle:@"确认" rightColor:[UIColor whiteColor]];
    _topView.backgroundColor = kThemeColor;
    [self addSubview:_topView];
    
    @weakify(self);
    _topView.touchButtonBlock = ^(EN_BUTTON_TYPE_TAG tag) {
        @strongify(self);
        if (tag == EN_BUTTON_TYPE_SUBMIT_TAG)
        {// 确认
            if ([objectNull(self.value) isEqualToString:@""])
            {
                [SVProgressHUD showInfoWithStatus:@"请选择！"];
                return;
            }
            if (self.viewBlock)
            {
                if (self.type == 0)
                {
                    self.viewBlock(self.value);
                }
                else
                {
                    self.viewBlock(@{@"value":objectNull(self.value), @"valueTwo":objectNull(self.valueTwo)});
                }
            }
            [self hide];
        }
        else
        {
            [self hide];
        }
    };
}

- (void)initWithPickerView
{
    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, _topView.bottom, kScreenWidth, 216)];
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
//    _pickerView.showsSelectionIndicator = YES;
    [_pickerView setAutoresizesSubviews:YES];
    [_pickerView setUserInteractionEnabled:YES];
    [_pickerView setContentMode:UIViewContentModeScaleToFill];
    [self addSubview:_pickerView];
}

- (void)updateWithViewData:(id)data
{
    if (self.type == 1)
    {
        NSArray *list = data;
        self.pickerList = [list objectOrNilAtIndex:0];
        self.pickerTwoList = [list objectOrNilAtIndex:1];
        self.valueTwo = self.pickerTwoList.firstObject;
    }
    else
    {
        self.pickerList = data;
    }
    self.value = self.pickerList.firstObject;
    
    
    
    [self.pickerView reloadAllComponents];
}

/// 重置
- (void)resetRealodData
{
    if (self.pickerList.count > 0)
    {
        [self.pickerView selectRow:0 inComponent:0 animated:YES];
    }
    if (self.pickerTwoList.count > 0)
    {
        [self.pickerView selectRow:0 inComponent:1 animated:YES];
    }
}

/// 移动到指定的位置
/// @param index index description
- (void)scrollPosition:(NSInteger)index
{
    [self.pickerView selectRow:index inComponent:0 animated:YES];
}

- (void)updateSelectData:(NSArray *)list
{
    NSString *one = list.firstObject;
    NSString *two = nil;
    
    NSInteger component = [self.pickerList indexOfObject:one];
    self.value = one;
    
    if (self.type == 1)
    {
        two = list.lastObject;
        self.valueTwo = two;
        NSInteger row = [self.pickerTwoList indexOfObject:two];
        [self.pickerView selectRow:row inComponent:1 animated:YES];
    }
    [self.pickerView selectRow:component inComponent:0 animated:YES];
    
}

- (void)show
{
    if (!kCSKeyWindow.custPickerView)
    {
        // 创建蒙版
        UIView *maskView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        maskView.backgroundColor = [UIColor blackColor];
        maskView.alpha = 0;
        [kCSKeyWindow addSubview:maskView];
        kCSKeyWindow.custPickerMaskView = maskView;
        
        [kCSKeyWindow addSubview:self];
        kCSKeyWindow.custPickerView = self;
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - kTYZBasePickerViewHeight;
        [UIView animateWithDuration:0.23 animations:^{
            maskView.alpha = 0.4;
            //            self.alpha = 1;
            self.frame = frame;
        }];
    }
}

- (void)hide
{
    if (kCSKeyWindow.custPickerView)
    {
//        [UIApplication sharedApplication].keyWindow
        @weakify(self);
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight;
        [UIView animateWithDuration:0.2 animations:^{
            @strongify(self);
            //            self.alpha = 0;
            self.frame = frame;
            kCSKeyWindow.custPickerMaskView.alpha = 0;
        } completion:^(BOOL finished) {
            [kCSKeyWindow.custPickerView removeFromSuperview];
            [kCSKeyWindow.custPickerMaskView removeFromSuperview];
            kCSKeyWindow.custPickerMaskView = nil;
            kCSKeyWindow.custPickerView = nil;
        }];
    }
}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if (self.type == 1)
    {
        return 2;
    }
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return self.pickerList.count;
            break;
        case 1:
            return [self.pickerTwoList count];
            break;
        default:
            return 0;
            break;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component)
    {
        case 0:
            return [self.pickerList objectOrNilAtIndex:row];
        case 1:
            return [self.pickerTwoList objectOrNilAtIndex:row];
            break;
        default:
            return @"";
            break;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//    self.value = [self.pickerList objectOrNilAtIndex:row];
//    if (self.viewBlock)
//    {
//        if (self.autoSelected)
//        {
//            self.viewBlock(self.value);
//        }
//    }
    
    
    
    switch (component)
    {
        case 0:
        {
//            TYZMonthDayPickerModel *model = [self.months objectOrNilAtIndex:row];
            self.value = [self.pickerList objectOrNilAtIndex:row];
//            [self.pickerView selectRow:0 inComponent:1 animated:YES];
//            [self.pickerView reloadComponent:1];
            
//            self.monthDayModel.month = model.month;
//            self.monthDayModel.day = model.days.firstObject;
        }  break;
        case 1:
            self.valueTwo = [self.pickerTwoList objectOrNilAtIndex:row];
            break;
        default:
            break;
    }
    if (self.viewBlock)
    {
        if (self.autoSelected)
        {
            if (self.type == 0)
            {
                self.viewBlock(self.value);
            }
            else
            {
                self.viewBlock(@{@"value":objectNull(self.value), @"valueTwo":objectNull(self.valueTwo)});
            }
        }
    }
}

@end
